<template>
  <!--    风控评分设置-->
  <Dialog :visible="visible" :title="dialogTitle" width="60%" v-show="$permiss[684]"
          @confirm="visibleConfirm"
          @cancel="visibleClose"
          @close="visibleClose"
          :confirm-loading="loading"
  >
    <el-tabs v-model="tabsActive">
      <el-tab-pane name="score" :label="$t('风控评分查询设置')" v-if="$permiss[682]">
        <div style="padding: 10px">
          <div>
            <span class="lable">{{ $t('新客') }}</span>
            <el-radio-group v-model="scoreForm.newGuest">
              <el-radio v-for="item in scoreConfigList" v-if="item.type === 1" :key="item.id" :label="item.id">
                {{ item.scoreName }}
              </el-radio>
            </el-radio-group>
          </div>
          <div>
            <span class="lable">{{ $t('老客') }}</span>
            <el-radio-group v-model="scoreForm.oldGuest">
              <el-radio v-for="item in scoreConfigList" v-if="item.type === 2" :key="item.id" :label="item.id">
                {{ item.scoreName }}
              </el-radio>
            </el-radio-group>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane name="sms" :label="$t('短信/语音设置')" v-if="$permiss[869]">
        <div style="padding: 10px">
          <div>
            <span class="lable">{{ $t('验证码短信') }}</span>
            <el-radio-group v-model="smsForm.codeSMS">
              <el-radio v-for="item in smsConfigList" v-if="item.type === 1" :key="item.id" :label="item.id">
                {{ item.smsName }}
              </el-radio>
            </el-radio-group>
          </div>
          <div>
            <span class="lable">{{ $t('业务短信') }}</span>
            <el-radio-group v-model="smsForm.serviceSMS">
              <el-radio v-for="item in smsConfigList" v-if="item.type === 2" :key="item.id" :label="item.id">
                {{ item.smsName }}
              </el-radio>
            </el-radio-group>
          </div>
          <div>
            <span class="lable">{{ $t('语音验证码') }}</span>
            <el-radio-group v-model="smsForm.voiceSMS">
              <el-radio v-for="item in smsConfigList" v-if="item.type === 4" :key="item.id" :label="item.id">
                {{ item.smsName }}
              </el-radio>
            </el-radio-group>
          </div>
          <div>
            <span class="lable">{{ $t('IVR挂机短信') }}</span>
            <el-radio-group v-model="smsForm.hookSms">
              <el-radio v-for="item in smsConfigList" v-if="item.type === 5" :key="item.id" :label="item.id">
                {{ item.smsName }}
              </el-radio>
            </el-radio-group>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane name="operate" :label="$t('运营商认证设置')" v-if="$permiss[874]">
        <div>
          <span> {{ $t('同一用户（手机号）在平台下任一APP中完成运营商认证后') }}，</span>
          <el-input @input="operatorChange" oninput="value = value.replace(/[^\d]/g, '')" v-model="operatorForm.num"
                    style="width: 80px;margin-right: 5px"/>
          <span>{{ $t('天有效期内无需重复查询，可直接共享到所有租户的任一APP') }}；</span>
        </div>
      </el-tab-pane>
      <el-tab-pane name="black" :label="$t('黑名单设置')" v-if="$permiss[873]">
        <div style="display: flex">
          <el-checkbox-group @change="blackChange" v-model="blackForm.num">
            <el-checkbox v-for="item in blackConfigList" :key="item.id" :label="item.id">
              {{ item.riskName }}
            </el-checkbox>
          </el-checkbox-group>
        </div>
      </el-tab-pane>
      <el-tab-pane name="head" :label="$t('多头设置')" v-if="$permiss[872]">
        <div style="display: flex">
          <el-radio-group @change="rickChange" v-model="rickForm.num">
            <el-radio v-for="item in rickConfigList" :key="item.id" :label="item.id">
              {{ item.riskName }}
            </el-radio>
          </el-radio-group>
        </div>
      </el-tab-pane>
      <el-tab-pane name="mail" :label="$t('邮件商设置')" v-if="$permiss[1147]">
        <div style="padding: 10px">
          <div>
            <span class="lable">{{ $t('(自动)贷前模板') }}</span>
            <el-radio-group v-model="mailForm.autoBefore">
              <el-radio v-for="item in mailConfigList" v-if="item.type === 1" :key="item.merchant"
                        :label="item.merchant">
                {{ item.merchant === 1 ? 'RushMail' : $t('思齐Mail') }}
              </el-radio>
            </el-radio-group>
          </div>
          <div>
            <span class="lable">{{ $t('(自动)注册未申请') }}</span>
            <el-radio-group v-model="mailForm.autoRegister">
              <el-radio v-for="item in mailConfigList" v-if="item.type === 3" :key="item.merchant"
                        :label="item.merchant">
                {{ item.merchant === 1 ? 'RushMail' : $t('思齐Mail') }}
              </el-radio>
            </el-radio-group>
          </div>
          <div>
            <span class="lable">{{ $t('(自动)贷后模板') }}</span>
            <el-radio-group v-model="mailForm.autoAfter">
              <el-radio v-for="item in mailConfigList" v-if="item.type === 2" :key="item.merchant"
                        :label="item.merchant">
                {{ item.merchant === 1 ? 'RushMail' : $t('思齐Mail') }}
              </el-radio>
            </el-radio-group>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane name="clabe" :label="$t('校验clabe')" v-if="$permiss[872]">
        <div style="display: flex">
          <el-radio-group @change="clabeChange" v-model="clabeForm.num">
            <el-radio v-for="item in clabeConfigList" :key="item.id" :label="item.id">
              {{ item.riskName }}
            </el-radio>
          </el-radio-group>
        </div>
      </el-tab-pane>
      <el-tab-pane name="rfc" :label="$t('校验rfc')" v-if="$permiss[872]">
        <div style="display: flex">
          <el-radio-group @change="rfcChange" v-model="rfcForm.num">
            <el-radio v-for="item in rfcConfigList" :key="item.id" :label="item.id">
              {{ item.riskName }}
            </el-radio>
          </el-radio-group>
        </div>
      </el-tab-pane>
      <el-tab-pane name="curp" label="curp" v-if="$permiss[872]">
        <div style="display: flex">
          <el-radio-group @change="curpChange" v-model="curpForm.num">
            <el-radio v-for="item in curpConfigList" :key="item.id" :label="item.id">
              {{ item.riskName }}
            </el-radio>
          </el-radio-group>
        </div>
      </el-tab-pane>
      <el-tab-pane name="thirdProduct" label="语音IVR">
        <div>
          <span class="lable">{{ $t('提醒') }}</span>
          <el-radio-group v-model="thirdPartyForm.remind">
            <el-radio v-for="item in thirdPartyConfig" v-if="item.viceType === 1" :key="item.id" :label="item.id">
              {{ item.key }}
            </el-radio>
          </el-radio-group>
        </div>
        <div>
          <span class="lable">{{ $t('催收') }}</span>
          <el-radio-group v-model="thirdPartyForm.collection">
            <el-radio v-for="item in thirdPartyConfig" v-if="item.viceType === 2" :key="item.id" :label="item.id">
              {{ item.key }}
            </el-radio>
          </el-radio-group>
        </div>
        <div>
          <span class="lable">{{ $t('营销') }}</span>
          <el-radio-group v-model="thirdPartyForm.market">
            <el-radio v-for="item in thirdPartyConfig" v-if="item.viceType === 3" :key="item.id" :label="item.id">
              {{ item.key }}
            </el-radio>
          </el-radio-group>
        </div>
        <div>
          <span class="lable">{{ $t('电话验证') }}</span>
          <el-radio-group v-model="thirdPartyForm.verification">
            <el-radio v-for="item in thirdPartyConfig" v-if="item.viceType === 4" :key="item.id" :label="item.id">
              {{ item.key }}
            </el-radio>
          </el-radio-group>
        </div>
        <div>
          <span class="lable">{{ $t('贷后联系人群呼') }}</span>
          <el-radio-group v-model="thirdPartyForm.groupCall">
            <el-radio v-for="item in thirdPartyConfig" v-if="item.viceType === 5" :key="item.id" :label="item.id">
              {{ item.key }}
            </el-radio>
          </el-radio-group>
        </div>
      </el-tab-pane>
    </el-tabs>
  </Dialog>
</template>

<script>
import {
  updateScoreConfig,
  selectByType,
  selectMailMerchant,
  selectByProductName,
  updateProductSms,
  selectRiskByType,
  updateRisk,
  updateMailMerchant,
  selectThirdPartyConfig,
  updateThirdPartyConfig
} from '@/api/product'

export default {
  props: {
    visible: {
      default: false,
      type: Boolean
    },
    rows: Object
  },
  data() {
    return {
      tabsActive: 'score',
      scoreConfigList: [],
      scoreForm: {
        newGuest: 0,
        oldGuest: 0
      },
      smsForm: {
        codeSMS: 0,
        serviceSMS: 0,
        voiceSMS: 0,
        hookSms: 0
      },
      smsConfigList: [],
      rickConfigList: [],
      rickForm: {
        num: []
      },
      blackConfigList: [],
      blackForm: {
        num: []
      },
      operatorConfigList: [],
      operatorForm: {
        num: 0
      },
      loading: false,
      clabeConfigList: [],
      clabeForm: {
        num: 0
      },
      rfcConfigList: [],
      rfcForm: {
        num: 0
      },
      curpConfigList: [],
      curpForm: {
        num: 0
      },
      mailConfigList: [],
      mailForm: {},
      thirdPartyForm: {},
      thirdPartyConfig: [],
    }
  },
  computed: {
    dialogTitle() {
      return this.rows.productName + ' - ' + this.$t('进件设置')
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.getScoreConfig()
        this.getSMSConfig()
        this.getRiskConfig(1)
        this.getRiskConfig(2)
        this.getRiskConfig(3)
        this.getRiskConfig(4)
        this.getRiskConfig(5)
        this.getRiskConfig(6)
        this.getMailConfig()
        this.getThirdPartyConfig(1)
      }
    }
  },
  methods: {
    // 获取风控评分
    async getScoreConfig() {
      const params = {
        productName: this.rows.productName
      }
      const res = await selectByType(params)
      if (res.code === 1) {
        this.scoreConfigList = res.data
        if (this.scoreConfigList.length) {
          // 默认选中
          const defaultSelect = this.scoreConfigList.filter(item => item.status === 1)
          // 新客默认选中
          const newGuest = defaultSelect.filter(item => item.type === 1)
          // 老客默认选中
          const oldGuest = defaultSelect.filter(item => item.type === 2)
          this.scoreForm = {
            newGuest: newGuest.length ? newGuest[0].id : this.scoreConfigList[0].id,
            oldGuest: oldGuest.length ? oldGuest[0].id : this.scoreConfigList[0].id,
          }
        }
      }
    },
    // 风控提交
    async scoreConfirm() {
      const selectRadio = this.scoreConfigList.map(item => {
        // 选中状态 1 取消状态0
        if (item.type === 1 && item.id === this.scoreForm.newGuest) {
          item.status = 1
        } else if (item.type === 1) {
          item.status = 0
        }
        if (item.type === 2 && item.id === this.scoreForm.oldGuest) {
          item.status = 1
        } else if (item.type === 2) {
          item.status = 0
        }
        return item
      })
      const res = await updateScoreConfig(selectRadio)
      if (res.code === 1) {
        return Promise.resolve(res)
      } else {
        return Promise.reject(res)
      }
    },
    // 获取邮件商列表
    async getMailConfig() {
      const res = await selectMailMerchant({productName: this.rows.productName})
      if (res.code === 1) {
        this.mailConfigList = res.data
        this.mailForm = {
          autoBefore: this.mailConfigList.find(item => item.checked && item.type === 1).merchant,
          autoRegister: this.mailConfigList.find(item => item.checked && item.type === 3).merchant,
          autoAfter: this.mailConfigList.find(item => item.checked && item.type === 2).merchant
        }
      }
    },
    // 获取短信设置
    async getSMSConfig() {
      const res = await selectByProductName({
        productName: this.rows.productName
      })
      if (res.code === 1) {
        this.smsConfigList = [...res.data]
        if (this.smsConfigList.length) {
          // 默认选中
          const defaultSelect = [...res.data].filter(item => item.status === 1)
          // 验证码短信默认选中
          const code = defaultSelect.filter(item => item.type === 1)
          // 业务短信默认选中
          const service = defaultSelect.filter(item => item.type === 2)
          const voice = defaultSelect.filter(item => item.type === 4)
          // IVR挂机短信默认选中
          const hookSms = defaultSelect.filter(item => item.type === 5)
          this.smsForm = {
            codeSMS: code.length ? code[0].id : this.smsConfigList.filter(i => i.type === 1)[0].id,
            serviceSMS: service.length ? service[0].id : this.smsConfigList.filter(i => i.type === 2)[0].id,
            voiceSMS: voice.length ? voice[0].id : null,
            hookSms: hookSms.length ? hookSms[0].id : null,
          }
        }
      }
    },
    //  短信设置提交
    async SMSConfirm() {
      const selectRadio = this.smsConfigList.map(item => {
        // 选中状态 1 取消状态0
        if (item.type === 1 && item.id === this.smsForm.codeSMS) {
          item.status = 1
        } else if (item.type === 1) {
          item.status = 0
        }
        if (item.type === 2 && item.id === this.smsForm.serviceSMS) {
          item.status = 1
        } else if (item.type === 2) {
          item.status = 0
        }
        if (item.type === 5 && item.id === this.smsForm.hookSms) {
          item.status = 1
        } else if (item.type === 5) {
          item.status = 0
        }
        return item
      })
      const res = await updateProductSms(selectRadio)
      if (res.code === 1) {
        return Promise.resolve(res)
      } else {
        return Promise.reject(res)
      }
    },

    // 获取第三方多头
    async getRiskConfig(type) {
      const res = await selectRiskByType({
        productName: this.rows.productName,
        riskType: type
      })
      if (res.code === 1) {
        const data = res.data
        if (type === 1) {
          // 黑名单设置
          this.blackConfigList = data
          // 默认选中
          const defaultSelect = this.blackConfigList.filter(item => item.status === 1)
          this.blackForm = {
            num: defaultSelect.map(item => item.id),
          }
        } else if (type === 2) {
          //第三方多头
          this.rickConfigList = data
          // 默认选中
          const defaultSelect = this.rickConfigList.filter(item => item.status === 1)
          this.rickForm = {
            num: defaultSelect.length ? defaultSelect[0].id : null
          }
        } else if (type === 3) {
          //运营商设置
          this.operatorConfigList = data
          this.operatorForm = {
            num: data && data.length ? data[0].status : 1
          }
        } else if (type === 4) {
          // 校验Clabe
          this.clabeConfigList = data
          // 默认选中
          const defaultSelect = this.clabeConfigList.filter(item => item.status === 1)
          this.clabeForm = {
            num: defaultSelect.length ? defaultSelect[0].id : null
          }
        } else if (type === 5) {
          // 校验rfc
          this.rfcConfigList = data
          const defaultSelect = this.rfcConfigList.filter(item => item.status === 1)
          this.rfcForm = {
            num: defaultSelect.length ? defaultSelect[0].id : null
          }
        } else if (type === 6) {
          // Curp
          this.curpConfigList = data
          const defaultSelect = this.curpConfigList.filter(item => item.status === 1)
          this.curpForm = {
            num: defaultSelect.length ? defaultSelect[0].id : null
          }
        }
      }
    },
    // 获取第三方产品配置
    async getThirdPartyConfig(type) {
      const res = await selectThirdPartyConfig({
        productName: this.rows.productName,
        type: type
      })
      if (res.code === 1) {
        // 默认选中
        const remind = res.data.find(item => item.viceType === 1 && item.value === '1')
        const collection = res.data.find(item => item.viceType === 2 && item.value === '1')
        const market = res.data.find(item => item.viceType === 3 && item.value === '1')
        const verification = res.data.find(item => item.viceType === 4 && item.value === '1')
        const groupCall = res.data.find(item => item.viceType === 5 && item.value === '1')
        this.thirdPartyForm = {
          remind: remind ? remind.id : null,
          collection: collection ? collection.id : null,
          market: market ? market.id : null,
          verificationL: verification ? verification.id : null,
          groupCall: groupCall ? groupCall.id : null
        }
        this.thirdPartyConfig = res.data
      }
    },
    // 第三方产品配置提交
    async thirdProductConfirm() {
      const remind = this.thirdPartyConfig.filter(item => item.viceType === 1).map(item => {
        item.value = this.thirdPartyForm.remind === item.id ? '1' : '0'
        return item
      })
      const collection = this.thirdPartyConfig.filter(item => item.viceType === 2).map(item => {
        item.value = this.thirdPartyForm.collection === item.id ? '1' : '0'
        return item
      })
      const market = this.thirdPartyConfig.filter(item => item.viceType === 3).map(item => {
        item.value = this.thirdPartyForm.market === item.id ? '1' : '0'
        return item
      })
      const verification = this.thirdPartyConfig.filter(item => item.viceType === 4).map(item => {
        item.value = this.thirdPartyForm.verification === item.id ? '1' : '0'
        return item
      })
      const groupCall = this.thirdPartyConfig.filter(item => item.viceType === 5).map(item => {
        item.value = this.thirdPartyForm.groupCall === item.id ? '1' : '0'
        return item
      })
      const params = [
        ...remind,
        ...collection,
        ...market,
        ...verification,
        ...groupCall
      ]
      const res = await updateThirdPartyConfig(params)
      if (res.code === 1) {
        return Promise.resolve(res)
      } else {
        return Promise.reject(res)
      }
    },
    // 更改数数组值
    operatorChange(value) {
      this.operatorConfigList = this.operatorConfigList.map(i => {
        i.status = parseInt(value)
        return i
      })
    },
    //黑名单复选选框选中
    blackChange(list) {
      this.blackConfigList = this.blackConfigList.map(i => {
        const val = list.filter(item => i.id === item)
        if (val.length !== 0) {
          i.status = 1
        } else {
          i.status = 0
        }
        return i
      })
    },
    //多头复选框选中
    rickChange(id) {
      this.rickConfigList = this.rickConfigList.map(i => {
        if (i.id === id) {
          i.status = 1
        } else {
          i.status = 0
        }
        return i
      })
    },
    // 邮件商选中
    mailChange() {

    },
    //  curp选中
    curpChange(id) {
      this.curpConfigList = this.curpConfigList.map(i => {
        if (i.id === id) {
          i.status = 1
        } else {
          i.status = 0
        }
        return i
      })
    },
    //  rfc选中
    rfcChange(id) {
      this.rfcConfigList = this.rfcConfigList.map(i => {
        if (i.id === id) {
          i.status = 1
        } else {
          i.status = 0
        }
        return i
      })
    },
    //  clabe选中
    clabeChange(id) {
      this.clabeConfigList = this.clabeConfigList.map(i => {
        if (i.id === id) {
          i.status = 1
        } else {
          i.status = 0
        }
        return i
      })
    },
    async rickConfirm(list) {
      const res = await updateRisk(list)
      if (res.code === 1) {
        return Promise.resolve(res)
      } else {
        return Promise.reject(res)
      }
    },
    async mailConfirm() {
      let list = []
      Object.keys(this.mailForm).forEach(item => {
        switch (item) {
          case 'autoBefore':
            list.push({
              mailMerchant: this.mailForm[item],
              type: 1
            })
            break;
          case 'autoAfter':
            list.push({
              mailMerchant: this.mailForm[item],
              type: 2
            })
            break;
          case 'autoRegister':
            list.push({
              mailMerchant: this.mailForm[item],
              type: 3
            })
        }
      })
      list = list.map(item => {
        item.productName = this.rows.productName
        return item
      })
      const res = await updateMailMerchant(list)
      if (res.code === 1) {
        return Promise.resolve(res)
      } else {
        return Promise.reject(res)
      }
    },
    /**
     * 综合设置提交
     */
    async visibleConfirm() {
      let res
      try {
        this.loading = true
        switch (this.tabsActive) {
          case "score":
            // 风控评分
            res = await this.scoreConfirm()
            break;
          case "sms":
            // 短信设置
            res = await this.SMSConfirm()
            break;
          case 'operate' :
            // 运营商设置
            res = await this.rickConfirm(this.operatorConfigList)
            break;
          case 'black' :
            //黑名单设置
            res = await this.rickConfirm(this.blackConfigList)
            break;
          case 'head' :
            // 多头设置
            res = await this.rickConfirm(this.rickConfigList)
            break;
          case 'clabe' :
            // clabe设置
            res = await this.rickConfirm(this.clabeConfigList)
            break;
          case 'rfc' :
            // rfc设置
            res = await this.rickConfirm(this.rfcConfigList)
            break;
          case 'curp' :
            // curp设置
            res = await this.rickConfirm(this.curpConfigList)
            break;
          case 'mail' :
            // 邮件商设置
            res = await this.mailConfirm()
            break;
          case 'thirdProduct' :
            // 第三方产品配置
            res = await this.thirdProductConfirm()
        }
        if (res) {
          this.$message.success(this.$t('操作成功'))
          this.$emit('update:visible', false)
          this.$emit('confirm')
          this.loading = false
        }
      } catch {
        this.loading = false
      }
    },
    visibleClose() {
      this.$emit('update:visible', false)
    }
  },
  beforeDestroy() {
    this.$emit('update:visible', false)
  }
}
</script>

<style scoped>
.lable {
  margin-right: 10px;
  font-size: 18px;
  display: inline-block;
  flex: 1;
}
</style>
